package com.xiguthings.waka.service.maintainer.impl;

import com.xiguthings.waka.dao.DataBaseStatus;
import com.xiguthings.waka.dao.mapper.MaintainerMapper;
import com.xiguthings.waka.exception.BussinessException;
import com.xiguthings.waka.po.Maintainer;
import com.xiguthings.waka.po.MaintainerExample;
import com.xiguthings.waka.po.extpo.Page;
import com.xiguthings.waka.publicUtils.MD5Util;
import com.xiguthings.waka.publicUtils.StringUtils;
import com.xiguthings.waka.service.maintainer.IMaintainerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class MaintainerServiceImpl implements IMaintainerService {

    @Resource
    private MaintainerMapper maintainerMapper;


    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public int countByExample(MaintainerExample example) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int deleteByExample(MaintainerExample example) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int deleteByPrimaryKey(String id) throws Exception {

        return maintainerMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int insert(Maintainer t) throws Exception {

        String account = t.getAccount();
        String password = t.getPassword();

        if (StringUtils.isBlank(account) || StringUtils.isBlank(password)) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "用户名和密码都不能为空");
        }

        String id = StringUtils.getRandomUUID();
        t.setMaintainerId(id);
        String md5Password = MD5Util.getMD5Code(password);
        t.setPassword(md5Password);

        t.setStatus(DataBaseStatus.NORMAL);

        return maintainerMapper.insert(t);
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int insertSelective(Maintainer t) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<Maintainer> selectByExample(MaintainerExample example) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Maintainer selectByPrimaryKey(String id) throws Exception {

        return maintainerMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByExampleSelective(Maintainer t, MaintainerExample example) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByExample(Maintainer t, MaintainerExample example) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(Maintainer t) throws Exception {
        if (t == null) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "无法操作空数据");
        }

        String password = t.getPassword();
        if (StringUtils.isNotBlank(password)) {
            t.setPassword(MD5Util.getMD5Code(password));
        }

        return maintainerMapper.updateByPrimaryKeySelective(t);
    }

    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public int updateByPrimaryKey(Maintainer t) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page selectByPage(Page pageEntity, MaintainerExample example) throws Exception {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Maintainer selectByAccount(String account) throws Exception {
        MaintainerExample example = new MaintainerExample();
        example.createCriteria().andAccountEqualTo(account);
        List<Maintainer> list = maintainerMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
